// pages/mine/anchorWelfare/anchorWelfare_detail/anchorWelfare_detail.js
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    //主要信息
    balance: [],
    code: '',
    extract_time: 0,
    receive_time: 0,


    //参数
    withdraw: false,
    canvas_bg: '',
    rpx: '',
    share_img: '',

    zfb_account: '',
    apply_user: '',
    apply_amount: 1,
    suits_id: 0,
    package_id: 1,
    //弹框
    flag_rule: true,
    flag_canvas: true,
    flag_share: true,

    //多次点击
  },
  //显示规则
  click_showrule() {
    this.setData({
      flag_rule: false,
    })
  },
  //加载个人信息
  getInfo() {
    wx.request({
      url: app.globalData.url + 'api/common/register/info',
      data: {
        token: app.globalData.token,
      },
      method: 'POST',
      success: (e) => {
        console.log(e)
        let info = e.data.data;
        this.setData({
          info: info,
        })
      },
    })
  },
  //隐藏
  click_hide() {
    this.setData({
      flag_rule: true,
    })
  },
  //查看体现详情
  go_detail() {
    let that = this;
    wx.navigateTo({
      url: '../../welfareDetail/welfareDetail',
    })
  },
  //点击提现
  click_extract() {
    // console.log(1)
    let that = this,
      zfb_account = this.data.zfb_account,
      apply_user = this.data.apply_user,
      apply_amount = parseInt(this.data.apply_amount),
      package_id = parseInt(this.data.package_id),
      balance = this.data.balance,
      realMoney = this.data.realMoney,
      money = parseInt(realMoney);
    if (apply_amount <= "0") {
      wx.showToast({
        title: '提现金额须大于0',
        icon: 'none',
      })
    } else {
      if (zfb_account && apply_user && apply_amount && package_id) {
        // console.log('apply_amount+' + apply_amount)
        // console.log('balance+' + money)
        if (apply_amount > money) {
          // console.log(2)
          wx.showToast({
            title: '提现金额需小于可提现金额',
            icon: 'none',
          })
        } else if (0 < apply_amount <= money) {
          // console.log(3)
          let extract_time = that.data.extract_time;
          extract_time = extract_time + 1;
          that.setData({
            extract_time: extract_time
          })
          if (that.data.extract_time == 1) {
            // console.log(12332131)
            wx.showModal({
              title: '提现申请',
              content: '本次提现将消耗您' + balance.bb_rate * apply_amount + '波币，是否确认提现?',
              showCancel: true,
              cancelText: '取消',
              cancelColor: '#333',
              confirmText: '确认',
              confirmColor: '#e3be84',
              success: (e) => {
                if (e.confirm) {
                  this.extract(zfb_account, apply_user, apply_amount, package_id);
                }
              },
              complete: (e) => {
                let timer = setTimeout(() => {
                  that.setData({
                    extract_time: 0
                  })
                  clearTimeout(timer)
                }, 200)
              }
            })
          }
        } else {
          // console.log(4)
          console.log('none')
        }

      } else {
        wx.showToast({
          title: '请填写所有信息',
          icon: 'none',
        })
      }
    }



  },
  //账户
  inp_account(e) {
    // console.log(e.detail.value)
    let zfb_account = e.detail.value;
    this.setData({
      zfb_account: zfb_account,
    })
  },
  //姓名
  inp_name(e) {
    // console.log(e.detail.value)
    let apply_user = e.detail.value;
    this.setData({
      apply_user: apply_user,
    })
  },
  //金额
  inp_money(e) {
    console.log(e.detail.value)
    let apply_amount = e.detail.value;
    this.setData({
      apply_amount: apply_amount,
    })
  },
  //提现
  extract(zfb_account, apply_user, apply_amount, package_id) {
    let that = this;
    wx.request({
      url: app.globalData.url + 'api/invitation/user/price',
      data: {
        token: app.globalData.token,
        zfb_account: zfb_account,
        apply_user: apply_user,
        apply_amount: apply_amount,
        package_id,
        package_id
      },
      method: 'POST',
      success: function(e) {
        console.log(e)
        if (e.data.code != 0) {
          wx.showToast({
            title: e.data.message,
            mask: false,
            icon: 'none'
          })
        } else {
          wx.showModal({
            content: '请等待,已提交申请!',
            showCancel: false,
            confirmText: '好的',
            confirmColor: '#d0a76c',
            success: function(e) {
              if (e.confirm) {
                wx.navigateBack({
                  delta: 1,
                })
              }
            }
          })
        }
        that.balance();
      },
    })
  },
  click_select(e) {
    // console.log(e);
    let suits_id = e.currentTarget.dataset.suitsId,
      id = e.currentTarget.dataset.id,
      price = e.currentTarget.dataset.price;
    this.setData({
      suits_id: suits_id,
      package_id: id,
      apply_amount: price
    })

  },
  //获取我的额度 等信息
  balance() {
    let that = this;
    wx.request({
      url: app.globalData.url + 'api/invitation/my/balance',
      data: {
        token: app.globalData.token,
      },
      method: 'POST',
      success: function(e) {
        // console.log(e)
        let balance = e.data.data,
          realMoney = parseInt(balance.bb_balance) ? parseInt(balance.bb_balance) / e.data.data.bb_rate : 0;
        that.setData({
          balance: balance,
          realMoney: realMoney
        })
      },
    })
    wx.request({
      url: app.globalData.url + 'api/invitation/user/price/package',
      data: {
        token: app.globalData.token,
      },
      method: 'POST',
      success: (e) => {
        // console.log(e)
        let suits = e.data.data.list;
        this.setData({
          suits: suits
        })
      },
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {
    this.balance();
    this.getInfo();
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  }
})